package com.example.login.controller;


import com.example.login.entity.Collect;
import com.example.login.entity.Poem;
import com.example.login.entity.Poemsen;
import com.example.login.repository.CollectRepository;
import com.example.login.repository.PoemRepository;
import com.example.login.repository.RecommandRepository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/index")
public class RecommandController {
    @Resource
    private RecommandRepository recommandRepository;
    @Resource
    private CollectRepository collectRepository;
    @Resource
    private PoemRepository poemRepository;

    @PostMapping("/index") //随机推荐
    public Poemsen recommand(){
        Random random=new Random();
        Integer num=random.nextInt(60)+1;
        return recommandRepository.findByPoemid(num);
    }


    @PostMapping("/index1/{userid}") //基于用户的收藏推荐
    public String recommand1(@PathVariable Integer userid){
        List<Collect> collectList = collectRepository.findByUserid(userid);
        Random random = new Random();
        int k =random.nextInt(collectList.size());
        List<Poem> poem = poemRepository.findById(collectList.get(k).getPoemid());
        int k1 = random.nextInt(poem.size());
        List<Poem> poems = poemRepository.findByDynasty(poem.get(k1).getDynasty());
        int k2 = random.nextInt(poems.size());
        String[] strs = poems.get(k2).getParagraphs().split("。");
        int k3 = random.nextInt(strs.length);
        return strs[k3];
    }
}
